import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import Register from '../views/Register'
import MainStudent from '../views/MainStudent'
import Class from '../components/student/Class'
import Course from '../components/student/course'
import Work from './../components/student/work'
// import studentpersonalSetting from '../components/student/personalSetting'

import userForm from '../components/student/userForm'
import index from './../views/index'
import homePage from '../components/index/homePage'
import course from '../components/index/course'
import help from '../components/index/help'
import productDynamic from '../components/index/productDynamic'

import teacherIndex from '../components/teacher/index'
import teacherClass from '../components/teacher/Class'
import TeacherCourse from '../components/teacher/course'
import TeacherPersonalSetting from '../components/teacher/userForm'
import teacherWork from '../components/teacher/work'

Vue.use(VueRouter)

const routes = [
  /* 欢迎页面 */
  {
    path: '/',
    component: index,
    children: [
      {
        path: '',
        component: homePage
      }
    ]
  },
  {
    path: '/index',
    name: 'index',
    component: index,
    children: [
      {
        path: '',
        component: homePage
      },
      {
        path: 'course',
        name: 'course',
        component: course
      },
      {
        path: 'help',
        name: 'help',
        component: help
      },
      {
        path: 'productDynamic',
        name: 'productDynamic',
        component: productDynamic
      }

    ]
  },
  /* d登陆页面 */
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  /* 注册页面 */
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  /* 学生界面 */
  {
    path: '/mainStudent',
    name: 'mainStudent',
    component: MainStudent,
    children: [
      {
        path: '',
        component: Class
      },
      {
        path: 'class',
        component: Class
      },
      {
        path: 'course',
        component: Course
      },
      {
        path: 'work',
        component: Work
      },
      {
        path: 'personalSetting',
        name: 'personalSetting',
        component: userForm
      }
    ]

  },
  {
    path: '/teacher',
    name: 'teacher',
    component: teacherIndex,
    children: [{
      path: '',
      component: teacherClass
    },
    {
      path: 'class',
      name: 'class',
      component: teacherClass
    },
    {
      path: 'course',
      name: 'course',
      component: TeacherCourse
    },
    {
      path: 'personalSetting',
      name: 'personalSetting',
      component: TeacherPersonalSetting
    },
    {
      path: 'work',
      name: 'work',
      component: teacherWork
    }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register' || to.path.includes('/index') || to.path === '/') {
    return next()
  } else {
    const username = sessionStorage.getItem('username')
    if (!username) {
      return next('/login')
    } else {
      return next()
    }
  }
})

export default router
